import { Component, OnInit } from "@angular/core";
import { Iwe7FormBuilderService } from "../iwe7-form-builder.service";
import { Router, ActivatedRoute } from "@angular/router";
@Component({
  selector: "iwe7-form-builder",
  templateUrl: "./iwe7-form-builder.component.html",
  styleUrls: ["./iwe7-form-builder.component.scss"]
})
export class Iwe7FormBuilderComponent implements OnInit {
  list: any[] = [];
  constructor(
    private builder: Iwe7FormBuilderService,
    private router: Router,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    this.formInit();
  }

  formMange(form: any) {
    this.builder.editForm(form);
    this.router.navigate(["/formBuilder/dataList"], {
      queryParams: { id: form.id || "0" }
    });
  }

  createTable(form: any) {
    this.router.navigate(["/formBuilder/createTable"], {
      queryParams: { id: form.id || "0" }
    });
  }

  previewForm(form: any) {
    this.router.navigate(["/formBuilder/preview"], {
      queryParams: { id: form.id || "0" }
    });
  }

  formInit() {
    this.builder.getList().subscribe((res: any) => {
      this.list = res;
    });
  }

  addForm() {
    this.router.navigate(["/formBuilder/createForm"]);
  }

  deleteForm(id: number) {
    this.builder.deleteForm(id).subscribe(res => {
      this.formInit();
    });
  }

  editForm(form: any) {
    this.builder.editForm(form);
    this.router.navigate(["/formBuilder/createForm"], {
      queryParams: { id: form.id }
    });
  }
}
